package com.runda.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.bbjob.mapper.SysOrganizationMapper;
import com.bbjob.model.ActivateCode;
import com.bbjob.model.SysOrganization;
import com.bbjob.util.service.impl.BaseService;
import com.github.pagehelper.PageHelper;
import com.runda.query.QueryActivateCode;
import com.runda.service.IActivateCodeService;

@Service
public class ActivateCodeServiceImpl extends BaseService<ActivateCode> implements IActivateCodeService{

	@Resource
	private SysOrganizationMapper sysOrganizationMapper;
	
	@Override
	public List<ActivateCode> selectByParams(QueryActivateCode activateCode) {
		
		PageHelper.startPage(activateCode.getPageNum(), activateCode.getPageSize());
		Example example = new Example(ActivateCode.class);
		Criteria c = example.createCriteria();
		
		if(activateCode.getId() != null) {
			c.andEqualTo("id", activateCode.getId());
		}
		
		if(StringUtils.isNotBlank(activateCode.getShadowCode())) {
			c.andEqualTo("shadowCode", activateCode.getShadowCode());
		}
		
		if(activateCode.getOrganId() != null) {
			c.andEqualTo("organId", activateCode.getOrganId());
		}
		
		List<ActivateCode> list = this.mapper.selectByExample(example);
		
		for (ActivateCode code : list) {
			
			SysOrganization organ = sysOrganizationMapper.selectByPrimaryKey(code.getOrganId());
			
			if (organ != null) {
				code.setOrganName(organ.getName());
			}
		}
		
		return list;
	}
	
}
